# Maestro Tournament Director Bot
# Copyright 2011 Ryan Chiu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#!/usr/bin/python
import telnetlib
import thread

import Command

class Connection:

    # Pretty self-explanatory - simply connects to the ICS,
    # enter username/password combo precisely when prompted
    def connect(self):
        self.telnet.open(self.server_host, self.server_port)

        # Print data until server prompts for a username, in which case it
        # will be entered.
        print self.telnet.read_until('login: ')
        self.connecting = True
        self.write_line(self.username)

        # Ditto for the password
        print self.telnet.read_until('password: ')
        self.write_line(self.password)

        print self.telnet.read_until(self.server_prompt)
        self.connecting = False
        self.connected = True
        
        while self.connected is True:
            line = self.telnet.read_until(self.server_prompt)
            thread.start_new_thread(self.parse_for_commands, (line,))    
            print line
            continue

    def __init__(self, server_host, server_port, server_prompt,
                 username, password):
        self.telnet = telnetlib.Telnet(server_host, server_port)
      
        self.server_host = server_host
        self.server_port = server_port
        self.server_prompt = server_prompt
        self.username = username
        self.password = password

        # Some status booleans for ease of error handling
        self.connected = False
        self.connecting = False

    def parse_for_commands(self, oldstr):
        if " tells you: " in oldstr:
            user = oldstr[oldstr.rfind("\n"):oldstr.find(" tells you: ")]
            mess = oldstr.replace(user+" tells you: ", "", 1)
            command_arr = mess.split(" ")
            command = command_arr[0]
            args = mess.replace(command+" ", "")

            self.commands = Command.Command()
            newstr = self.commands.parse_tell(command, args)
            self.tell(user, newstr)

    def tell(self, user, msg):
        self.write_line("tell" + str(user) + " " + str(msg))
        
    def write_line(self, oldstr):
        oldstr = oldstr + "\n"
        self.telnet.write(oldstr)

    # Some getter/setter methods that I thought might be necessary
    def get_server_host(self): return self.server_host
    def get_server_prompt(self): return self.server_prompt
    def get_server_port(self): return self.server_port

    def is_connected(self): return self.connected
    def is_connecting(self): return self.connecting
    
    def set_server_host(self, host): self.server_host = host
    def set_server_prompt(self, prompt): self.server_prompt = prompt
    def set_server_port(self, port): self.server_port = port
